package com.maigang.controller.customer;

import com.maigang.pojo.common.User;
import com.maigang.pojo.vo.CartVo;
import com.maigang.service.customer.ICartService;
import com.maigang.utils.common.Const;
import com.maigang.utils.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: CartController
 * @Description: 购物车模块控制器
 * @author: yubao.jin
 * @date: 2018/4/13
 * @version:V1.0
 */
@Controller
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    private ICartService iCartService;

    @RequestMapping("cart")
    public ModelAndView goToCart(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cart/cart.ftl");
        modelAndView.addObject("serverResponse", getCartlist(session, 1, 6));
        return modelAndView;
    }

    @RequestMapping("list")
    @ResponseBody
    public ServerResponse<CartVo> getCartlist(HttpSession session,
                                       @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.list(2, pageNum, pageSize);
    }

    @RequestMapping("add")
    @ResponseBody
    public ServerResponse<CartVo> addProduct(HttpSession session, Integer quantity, Integer productId, Integer isPresale,
                                      @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.add(2, productId, quantity, isPresale, pageNum, pageSize);
    }

    @RequestMapping("update")
    @ResponseBody
    public ServerResponse<CartVo> updateProduct(HttpSession session, Integer quantity, Integer cartId,
                                         @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.update(2, cartId, quantity, pageNum, pageSize);
    }

    @RequestMapping("delete")
    @ResponseBody
    public ServerResponse<CartVo> deleteProduct(HttpSession session, String cartIds,
                                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.deleteProduct(2, cartIds, pageNum, pageSize);
    }

    // 全选
    @RequestMapping("select_all")
    @ResponseBody
    public ServerResponse<CartVo> selectAll(HttpSession session,
                                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                                            String cartIds){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.selectOrUnselect(2, null, Const.Cart.CHECKED, pageNum, pageSize, cartIds);
    }

    // 全反选
    @RequestMapping("un_select_all")
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(HttpSession session,
                                              @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                                              String cartIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.selectOrUnselect(2, null, Const.Cart.UNCHECKED, pageNum, pageSize, cartIds);
    }

    // 单独选
    @RequestMapping("select")
    @ResponseBody
    public ServerResponse<CartVo> select(HttpSession session, Integer productId,
                                         @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                                         String cartIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.selectOrUnselect(2, productId, Const.Cart.CHECKED, pageNum, pageSize, cartIds);
    }

    // 单独反选
    @RequestMapping("un_select")
    @ResponseBody
    public ServerResponse<CartVo> unSelect(HttpSession session, Integer productId,
                                           @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                                           String cartIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.selectOrUnselect(2, productId, Const.Cart.UNCHECKED, pageNum, pageSize, cartIds);
    }

    @RequestMapping("get_cart_product_count")
    @ResponseBody
    public ServerResponse<Integer> getCartProductCount(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iCartService.getCartProductCount(2);
    }
}
